package com.example.springbooteasycode.test.toC;

import jnr.ffi.LibraryLoader;

public class JnrExample {
    public static void main(String[] args) {
        // 加载 C 库
        LibC libc = LibraryLoader.create(LibC.class).load("c");

        // 调用 time 函数
        long currentTime = libc.time(0);
        System.out.println("Current time: " + currentTime);

        // 调用 strcpy 函数
        byte[] buffer = new byte[100];
        libc.strcpy(buffer, "Hello from JNR");
        System.out.println(new String(buffer).trim());

        // 调用文件操作函数
        int fd = libc.open("/tmp/test.txt", 0);
        if (fd != -1) {
            byte[] data = new byte[1024];
            int bytesRead = libc.read(fd, data, data.length);
            libc.close(fd);
            System.out.println("Read " + bytesRead + " bytes");
        }
    }
}
